<?php
namespace Cyfy\Modules\ListBoxes\Forms;

use \Cyfy\Modules\Forms;
use \Cyfy\Modules\ListBoxes\Module;

/**
  *	Create a new listbox
  */
class NewBox extends Forms\Form{

	function structure()
	{
        $this -> permission("ListBoxes", "administration");
        
        $i = new Forms\TextBox("name", self :: t(10));
        $i -> required = true;
        $this -> addItem($i);
        
        $i = new Forms\TextBox("code", self :: t(20));
        $i -> description = self :: t(60);
        $i -> required = true;
        $this -> addItem($i);
        
        $b = new Forms\Button(self :: t(30));
        $this -> addButton($b);
	}
	
	function validate()
	{
		if(preg_match("/^[0-9A-Za-z-]+$/", $this -> get("code")) == false)
        {
			\Cyfy\Message :: set(self :: t(40), \Cyfy\Message :: ERROR);
			return false;
		}
		return true;
	}
	
    
	function submit()
	{
        $values = array(
            "name" => $this->get("name"),
            "code" => $this->get("code")
        );
     	\dibi :: query("INSERT INTO [:cyfy:ListBoxes-boxes] %v", $values);
        \Cyfy\Cyfy ::  makeDirectory(\Cyfy\Cyfy ::  $filesDir . Module :: DIR . \dibi :: insertId());
        
        $this -> onSubmit = "Cyfy.ajax('ListBoxes', 'listOfBoxes', '#listOfListBoxes');";
		\Cyfy\Message :: set(self :: t(51, array($this -> get("code"))));
		return true;
	}
}